package com.xing.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.xing.dao.ContractDao;
import com.xing.model.ContractInfo;
import com.xing.model.DicPayment;
import com.xing.util.Util;

@Service
public class ContractService {

	private static Logger logger = Logger.getLogger(ContractService.class);
	
	@Resource
	private ContractDao dao;
	
	public void setDao(ContractDao dao) {
		this.dao = dao;
	}
	
	public Map<String, Object> getContractInfo(int id) {
		
		logger.info("do getContractInfo");
		
		List<String> conditionList = new ArrayList<String>();
		conditionList.add("c.contractId=" + id);
		
		List<Map<String, Object>> list = (List<Map<String, Object>>) dao.getListAndCount(conditionList, null, null).get("rows");
		if (!Util.isEmpty(list)) {
			
			List<Map<String, Object>> cpList = dao.getContractPaymentList(id);

			Map<String, Object> result = list.get(0);
			result.put("cpList", cpList);
			
			return result;
		} else {
			return null;
		}
	}

	public void addContract(ContractInfo info) {
		
		dao.addContract(info);
	}
	
	public void updateContract(ContractInfo info) {
		
		// 先删除现有的payment
		List<Integer> idList = new ArrayList<Integer>();
		idList.add(info.getContractId());
		dao.deleteContractPayment(idList);
		
		dao.updateContract(info);
	}
	
	public void deleteContract(String idStr) {
		
		String[] arr = idStr.split(",");
		List<Integer> idList = new ArrayList<Integer>();
		for (String tmp : arr) {
			idList.add(Integer.parseInt(tmp));
		}
		
		dao.deleteContract(idList);
		dao.deleteContractPayment(idList);
	}
	
	public Map<String, Object> getListAndCount(List<String> conditionList, int page, int pageRow) {
		
		logger.info("do getList");
		
		return dao.getListAndCount(conditionList, page, pageRow);
	}
	
	public List<DicPayment> getPaymentDicList() {
		
		logger.info("do getPaymentDicList");
		
		return dao.getPaymentDicList();
	}
}
